import BaseComponentByHall from "../../lib/Base/BaseComponentByHall";
import { glUtils } from "../../lib/GLUtils";
import ScrollSelect from "./ScrollSelector/ScrollSelector";


const { ccclass, property } = cc._decorator;
const MINYEAR = 1970
export enum DateType {
    year = 0,
    month = 1,
    day = 2,
}
@ccclass
export default class DatePicker extends BaseComponentByHall {

    @property(cc.Node)
    ScrollSelect_Root: cc.Node = null;
    @property(cc.Prefab)
    ScrollSelect_Prefab: cc.Prefab = null;


    ScrollSelect_Year_sc: ScrollSelect = null;
    ScrollSelect_Month_sc: ScrollSelect = null;
    ScrollSelect_Day_sc: ScrollSelect = null;


    YearList: number[] = []
    MonthList: number[] = []
    DayList: number[] = []


    _resYear
    _resMonth
    _resDay

    _cb
    onLoad() {
        this._initUI();
        this._init();
        this.onEvent("SV_SelectItem")
        this.onEvent("SV_getItem")
    }
    onDestroy() {
        this.offEvent("SV_SelectItem")
        this.offEvent("SV_getItem")

    }
    //接选中事件 -> 见 ScrollViewAdsorptionEffect._check 
    SV_SelectItem(datas) {
        let data = datas[0];
        if (!data || !data["getComponent"]) {
            glUtils.testErrorLog("获取到了一个错误的节点")
            return;
        }
        let sc = data.getComponent(data.name)
        if (!sc) {
            glUtils.testErrorLog("获取到了一个错误的节点")
            return;
        }
        let itemData = sc.data
        switch (itemData.type) {
            case DateType.year:
                this._resYear = itemData.value
                this._refreshDay(this._resYear, this._resMonth, this._resDay)
                break;
            case DateType.month:
                this._resMonth = itemData.value
                this._refreshDay(this._resYear, this._resMonth, this._resDay)
                break;
            case DateType.day:
                this._resDay = itemData.value
                break;
        }

    }
    SV_getItem(datas) {
        let data = datas[0];
        if (!data || !data["getComponent"]) {
            glUtils.testErrorLog("获取到了一个错误的节点")
            return;
        }
        let sc = data.getComponent(data.name)
        if (!sc) {
            glUtils.testErrorLog("获取到了一个错误的节点")
            return;
        }
        let itemData = sc.data
        switch (itemData.type) {
            case DateType.year:
                this._resYear = itemData.value
                break;
            case DateType.month:
                this._resMonth = itemData.value
                break;
            case DateType.day:
                this._resDay = itemData.value
                break;
        }

    }
    _initUI() {
        this.ScrollSelect_Year_sc = this.createNode(this.ScrollSelect_Prefab, this.ScrollSelect_Root);
        this.ScrollSelect_Month_sc = this.createNode(this.ScrollSelect_Prefab, this.ScrollSelect_Root);
        this.ScrollSelect_Day_sc = this.createNode(this.ScrollSelect_Prefab, this.ScrollSelect_Root);
    }
    /** 初始化 已今天的为准 */
    _init() {
        let date = new Date();
        let len = date.getFullYear()
        for (let i = MINYEAR; i <= len; i++) {
            this.YearList.push(i)
        }
        this.ScrollSelect_Year_sc.initData(this.YearList, DateType.year)
        let mlen = 12
        for (let i = 1; i <= mlen; i++) {
            this.MonthList.push(i)
        }
        this.ScrollSelect_Month_sc.initData(this.MonthList, DateType.month)

        let dlen = 31;
        for (let i = 1; i <= dlen; i++) {
            this.DayList.push(i)
        }
        this.ScrollSelect_Day_sc.initData(this.DayList, DateType.day)

        this.setDate(date.getFullYear(), date.getMonth() + 1, date.getDate())

    }
    //取日的天
    _getDayList(year, month): number[] {
        let res: number[] = [];
        let date = new Date(year, month, 0)
        let maxDay = date.getDate();
        let len = maxDay
        for (let i = 1; i <= len; i++) {
            res.push(i)
        }
        return res;
    }
    //刷新日
    _refreshDay(year, month, oldDay) {
        //重算日
        let temp = this._getDayList(year, month)
        if (temp.length != this.DayList.length) {
            this.DayList = temp
            this.ScrollSelect_Day_sc.initData(this.DayList, DateType.day)
        }
        //重设日
        let length = temp.length
        if (oldDay < length) {
            this.ScrollSelect_Day_sc.scrollToIndex(oldDay - 1, length - 1, 0.2)
        } else {
            this.ScrollSelect_Day_sc.scrollToIndex(length - 1, length - 1, 0.2)
        }
    }
    /** 设置时间
     * 
     * @param year 
     * @param month 真月份  12月->12
     * @param day 
     */
    setDate(year, month, day) {
        this._resYear = year
        this._resMonth = month
        this._resDay = day
        this.ScrollSelect_Year_sc.scrollToIndex(year - MINYEAR, this.YearList.length - 1)
        this.ScrollSelect_Month_sc.scrollToIndex(month - 1, this.MonthList.length - 1)
        this._refreshDay(year, month, day)

    }
    setPickDateCallback(cb) {
        this._cb = cb;
    }

    on_btn_close_click() {
        this.node.destroy();
    }
    on_btn_confirm_click() {
        cc.log("this._resYear, this._resMonth, this._resDay", this._resYear, this._resMonth, this._resDay)
        this.ScrollSelect_Day_sc.getNowSelectItem()
        this.ScrollSelect_Month_sc.getNowSelectItem()
        this.ScrollSelect_Year_sc.getNowSelectItem()
        if (this._cb) {
            this._cb(this._resYear, this._resMonth - 1, this._resDay)
        }
        this.node.destroy();
    }


}
